import OpenAI from 'openai'

const openai = new OpenAI({
  apiKey: '058617fd-cd49-45cd-82ac-4e6791631f8b',
  baseURL: 'https://ark.cn-beijing.volces.com/api/v3'
})

async function main() {
  // Non-streaming:
  console.log('----- standard request -----')
  const completion = await openai.chat.completions.create({
    messages: [
      { role: 'system', content: '你是豆包，是由字节跳动开发的 AI 人工智能助手' },
      { role: 'user', content: '常见的十字花科植物有哪些？' }
    ],
    model: 'ep-20250212154047-x6hz6'
  })
  console.log(completion.choices[0]?.message?.content)

  // Streaming:
  console.log('----- streaming request -----')
  const stream = await openai.chat.completions.create({
    messages: [
      { role: 'system', content: '你是豆包，是由字节跳动开发的 AI 人工智能助手' },
      { role: 'user', content: '常见的十字花科植物有哪些？' }
    ],
    model: 'ep-20250212154047-x6hz6',
    stream: true
  })
  for await (const part of stream) {
    process.stdout.write(part.choices[0]?.delta?.content || '')
  }
  process.stdout.write('\n')
}

main()
